<template>
  <div>
    <el-card class="box-card">
      <span>{{ id ? '编辑' : '添加' }}</span>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="菜单路径" prop="name">
          <el-input v-model="ruleForm.href"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单">
          <el-select v-model="ruleForm.parentId">
            <el-option label="无上级菜单" :value="-1"></el-option>
            <el-option :label="item.name" :value="item.id" v-for="item in parentList" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="delivery">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="前端图标" prop="delivery">
          <el-input v-model="ruleForm.icon"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="resource">
          <el-radio-group v-model="ruleForm.shown">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="desc">
          <el-input-number v-model="ruleForm.orderNum" :min="1" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import Vue from 'vue'
import { createMenu, getEditMenuInfo, getMenu } from '@/apis/menu'
export default Vue.extend({
  data () {
    return {
      ruleForm: {
        parentId: -1, // 表示没有上级菜单
        name: '',
        href: '',
        description: '',
        icon: '',
        shown: false,
        orderNum: ''
      },
      rules: {},
      parentList: [],
      id: ''
    }
  },
  async created () {
    this.id = this.$route.query.id as string
    if (this.id) {
      const { data } = await getMenu(this.id)
      this.ruleForm = data
    }
    this.getEditMenuInfo()
  },
  methods: {
    async getEditMenuInfo () {
      const res: any = await getEditMenuInfo()
      if (res.code === '000000') {
        this.parentList = res.data.parentMenuList
      }
    },
    async submitForm () {
      // this.$refs.ruleForm.validate()
      await createMenu(this.ruleForm)
      this.$router.back()
      this.$message.success(this.id ? '编辑成功' : '创建成功')
    }
  }
})
</script>

<style lang="scss">
.el-input {
  // width: 400px;
}
</style>
